<template>
  <div>
    <div v-if="list.length > 0">
      <div class="test">
        <span style="color: #ccc">历史记录</span>
        <van-icon @click="dels" class="right" name="delete-o" size="18" color="#999" />
      </div>
      <div class="test" v-for="(item, index) in list" :key="index" @click="searchHistory(item)">
        <span>{{item}}</span>
        <van-icon @click.stop="del(item)" class="right" name="cross" size="18" color="#999" />
      </div>
    </div>
    <div class="test" v-else>
      <span style="color: #ccc">暂无历史记录</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'History',
  data () {
    return {
      list: JSON.parse(localStorage.getItem('historyList')) || []
    }
  },
  methods: {
    dels () {
      localStorage.removeItem('historyList')
      this.list = []
    },
    del (item) {
      this.list = JSON.parse(localStorage.getItem('historyList')).filter(val => item !== val)
      localStorage.setItem('historyList', JSON.stringify(this.list))
    },
    searchHistory (test) {
      this.$emit('searchHot', test)
    }
  }
}
</script>

<style lang="less" scoped>
.test {
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
span {
  margin: 15px 0 15px 15px;
}
/deep/span {
  font-size: 32px;
}
</style>
